<?php
namespace app\controller;

use app\validate\User as UserValidate;
use think\exception\ValidateException;
use think\facade\View;

class User extends Base
{
    protected $middleware = ['checkAdmin'];

    /**
     * 初始化
     */
    protected function initialize()
    {
        parent::initialize();
        $this->logic = new \app\logic\User();
    }

    /**
     * 资料修改
     */
    public function profile()
    {
        if(request()->isAjax()) {
            $param = request()->param(['username', 'old_password', 'password'], [], 'trim,strip_tags');
            // 验证数据
            try {
                validate(UserValidate::class)->scene('profile')->check($param);
            } catch (ValidateException $e) {
                return $this->jsonReturn(config('status.verify'), $e->getError());
            }

            return $this->jsonReturn($this->logic->profile($param));
        }

        return view("", [
            'user' => (new \app\model\User())->getOneInfoByWhere(['id' => session('user')['id'] ?? 0], "id, username")
        ]);
    }
}